package cn.com.infohold.smartcity.activity.Warrant;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.api.bean.WarrantBean;
import cn.com.infohold.smartcity.api.bean.WarrantDetailResult;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class WarrantDetail_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.tv_docnumber)
    TextView tv_docnumber;
    @ViewInject(R.id.tv_contractnumber)
    TextView tv_contractnumber;
    @ViewInject(R.id.tv_fullname)
    TextView tv_fullname;
    @ViewInject(R.id.tv_contractname)
    TextView tv_contractname;
    @ViewInject(R.id.tv_contractcode)
    TextView tv_contractcode;
    @ViewInject(R.id.tv_contractorname)
    TextView tv_contractorname;
    @ViewInject(R.id.tv_contractorcode)
    TextView tv_contractorcode;
    @ViewInject(R.id.tv_cardid)
    TextView tv_cardid;
    @ViewInject(R.id.tv_tel)
    TextView tv_tel;
    @ViewInject(R.id.tv_address)
    TextView tv_address;
    @ViewInject(R.id.tv_contracttype)
    TextView tv_contracttype;
    @ViewInject(R.id.tv_landnumber)
    TextView tv_landnumber;
    @ViewInject(R.id.tv_area)
    TextView tv_area;
    @ViewInject(R.id.tv_startdate)
    TextView tv_startdate;
    @ViewInject(R.id.tv_enddate)
    TextView tv_enddate;
    @ViewInject(R.id.tv_certificatedate)
    TextView tv_certificatedate;
    @ViewInject(R.id.tv_certificatecode)
    TextView tv_certificatecode;
    @ViewInject(R.id.tv_certificateoffice)
    TextView tv_certificateoffice;
    @ViewInject(R.id.tv_remark)
    TextView tv_remark;

    @ViewInject(R.id.rv_list)
    RecyclerView rv_list;
    @ViewInject(R.id.rv_appendlist)
    RecyclerView rv_appendlist;

    private WarrantBean warrantBean;
    private String warrantId;

    @Override
    protected Object inflateView() {
        return R.layout.activity_warrant_detail;
    }

    @Override
    protected void initView(View anchor) {

        Intent intent = getIntent();
        Bundle warrant_bundle = intent.getBundleExtra("warrant_bundle");
        if (warrant_bundle != null){
            warrantBean = (WarrantBean) warrant_bundle.getSerializable("warrant_detail");
            warrantId = warrantBean.getId();
            initData();
        }

    }

    private void initData() {
        SDK.api().getWarrantDetail(warrantId).postEvent(RequestCode.REQUEST_CODE_GET_WARRANT_DETAIL);
        ProgressDialog.show(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent){

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_GET_WARRANT_DETAIL) {
            ProgressDialog.dismiss();
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            ProgressDialog.dismiss();
            return;
        } else {
            switch (requestCode){
                case RequestCode.REQUEST_CODE_GET_WARRANT_DETAIL:
                    ProgressDialog.dismiss();
                    Type type = new TypeToken<LandResult<WarrantDetailResult>>(){}.getType();
                    LandResult<WarrantDetailResult> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    if (result.getStatus().equals("1")){
                        WarrantDetailResult warrantDetailResult = result.getResult();
                        tv_docnumber.setText(warrantDetailResult.getWarrant().getDocnumber());
                        tv_contractnumber.setText(warrantDetailResult.getWarrant().getContractnumber());
                        tv_fullname.setText(warrantDetailResult.getContracting_party().getFullname());
                        tv_contractname.setText(warrantDetailResult.getContracting_party().getName());
                        tv_contractcode.setText(warrantDetailResult.getContracting_party().getCode());
                        tv_contractorname.setText(warrantDetailResult.getContractor().getName());
                        tv_contractorcode.setText(warrantDetailResult.getContractor().getCode());
                        tv_cardid.setText(warrantDetailResult.getContractor().getCardid());
                        tv_tel.setText(warrantDetailResult.getContractor().getTel());
                        tv_address.setText(warrantDetailResult.getContractor().getAddress());
                        tv_contracttype.setText(warrantDetailResult.getWarrant().getContracttype());
//                        tv_landnumber.setText();
                        tv_area.setText(warrantDetailResult.getWarrant().getArea() + "");
                        tv_startdate.setText(warrantDetailResult.getWarrant().getStartdate());
                        tv_enddate.setText(warrantDetailResult.getWarrant().getEnddate());
                        tv_certificatedate.setText(warrantDetailResult.getWarrant().getCertificatedate());
                        tv_certificatecode.setText(warrantDetailResult.getWarrant().getCertificatecode());
                        tv_certificateoffice.setText(warrantDetailResult.getWarrant().getCertificateoffice());
                        tv_remark.setText(warrantDetailResult.getLand().get(0).getRemark());
                    }

                    break;
                default:
                    break;
            }
        }
    }

}
